home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Online / smbfs / source / crypt.c < prev    next >
C/C++ Source or Header  |  2000-12-17  |  17KB  |  725 lines

  1. /*
  2.  * $Id: crypt.c 1.4 2000/12/17 09:39:34 olsen Exp olsen $
  3.  *
  4.  * :ts=8
  5.  *
  6.  * Password encryption routines, lifted from the Samba source code,
  7.  * "libsmb/smbencrypt.c", "libsmb/smbdes.c" and "lib/md4.c":
  8.  *
  9.  * Unix SMB/Netbios implementation.
  10.  * Version 1.9.
  11.  *
  12.  * SMB parameters and setup
  13.  *
  14.  * A partial implementation of DES designed for use in the
  15.  * SMB authentication protocol
  16.  *
  17.  * An implementation of MD4 designed for use in the SMB authentication protocol
  18.  *
  19.  * Copyright (C) Andrew Tridgell 1992-1998
  20.  * Modified by Jeremy Allison 1995.
  21.  *
  22.  * This program is free software; you can redistribute it and/or modify
  23.  * it under the terms of the GNU General Public License as published by
  24.  * the Free Software Foundation; either version 2 of the License, or
  25.  * (at your option) any later version.
  26.  *
  27.  * This program is distributed in the hope that it will be useful,
  28.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  * GNU General Public License for more details.
  31.  *
  32.  * You should have received a copy of the GNU General Public License
  33.  * along with this program; if not, write to the Free Software
  34.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  */
  36.  
  37. #include "system_headers.h"
  38. #include "assert.h"
  39.  
  40. /****************************************************************************/
  41.  
  42. #include <smb/smb.h>
  43.  
  44. /****************************************************************************/
  45.  
  46. extern VOID StringToUpper(STRPTR s);
  47.  
  48. /****************************************************************************/
  49.  
  50. static void permute (char *out, char *in, const unsigned char * const p, int n);
  51. static void left_shift (char *d, int count, int n);
  52. static void concat (char *out, char *in1, char *in2, int l1, int l2);
  53. static void xor (char *out, char *in1, char *in2, int n);
  54. static void dohash (char *out, char *in, char *key, int forw);
  55. static void str_to_key (unsigned char *str, unsigned char *key);
  56. static void smbhash (unsigned char *out, unsigned char *in, unsigned char *key, int forw);
  57. static void E_P16 (unsigned char *p14, unsigned char *p16);
  58. static void E_P24 (unsigned char *p21, unsigned char *c8, unsigned char *p24);
  59. static int local_wcslen (short *str);
  60. static int local_mbstowcs (short *dst, unsigned char *src, int len);
  61. static void E_md4hash (unsigned char *passwd, unsigned char *p16);
  62. static void smb_owf_encrypt (unsigned char *passwd, unsigned char *c8, unsigned char *p24);
  63. static unsigned long F (unsigned long X, unsigned long Y, unsigned long Z);
  64. static unsigned long G (unsigned long X, unsigned long Y, unsigned long Z);
  65. static unsigned long H (unsigned long X, unsigned long Y, unsigned long Z);
  66. static unsigned long lshift (unsigned long x, int s);
  67. static void mdfour64 (unsigned long *M);
  68. static void copy64 (unsigned long *M, unsigned char *in);
  69. static void copy4 (unsigned char *out, unsigned long x);
  70. static void mdfour (unsigned char *out, unsigned char *in, int n);
  71.  
  72. /****************************************************************************/
  73.  
  74. /* NOTES:
  75.  
  76.    This code makes no attempt to be fast! In fact, it is a very
  77.    slow implementation
  78.  
  79.    This code is NOT a complete DES implementation. It implements only
  80.    the minimum necessary for SMB authentication, as used by all SMB
  81.    products (including every copy of Microsoft Windows95 ever sold)
  82.  
  83.    In particular, it can only do a unchained forward DES pass. This
  84.    means it is not possible to use this code for encryption/decryption
  85.    of data, instead it is only useful as a "hash" algorithm.
  86.  
  87.    There is no entry point into this code that allows normal DES operation.
  88.  
  89.    I believe this means that this code does not come under ITAR
  90.    regulations but this is NOT a legal opinion. If you are concerned
  91.    about the applicability of ITAR regulations to this code then you
  92.    should confirm it for yourself (and maybe let me know if you come
  93.    up with a different answer to the one above)
  94.  */
  95.  
  96. static const unsigned char perm1[56] =
  97. {
  98.  57, 49, 41, 33, 25, 17, 9,
  99.  1, 58, 50, 42, 34, 26, 18,
  100.  10, 2, 59, 51, 43, 35, 27,
  101.  19, 11, 3, 60, 52, 44, 36,
  102.  63, 55, 47, 39, 31, 23, 15,
  103.  7, 62, 54, 46, 38, 30, 22,
  104.  14, 6, 61, 53, 45, 37, 29,
  105.  21, 13, 5, 28, 20, 12, 4
  106. };
  107.  
  108. static const unsigned char perm2[48] =
  109. {
  110.  14, 17, 11, 24, 1, 5,
  111.  3, 28, 15, 6, 21, 10,
  112.  23, 19, 12, 4, 26, 8,
  113.  16, 7, 27, 20, 13, 2,
  114.  41, 52, 31, 37, 47, 55,
  115.  30, 40, 51, 45, 33, 48,
  116.  44, 49, 39, 56, 34, 53,
  117.  46, 42, 50, 36, 29, 32
  118. };
  119.  
  120. static const unsigned char perm3[64] =
  121. {
  122.  58, 50, 42, 34, 26, 18, 10, 2,
  123.  60, 52, 44, 36, 28, 20, 12, 4,
  124.  62, 54, 46, 38, 30, 22, 14, 6,
  125.  64, 56, 48, 40, 32, 24, 16, 8,
  126.  57, 49, 41, 33, 25, 17, 9, 1,
  127.  59, 51, 43, 35, 27, 19, 11, 3,
  128.  61, 53, 45, 37, 29, 21, 13, 5,
  129.  63, 55, 47, 39, 31, 23, 15, 7
  130. };
  131.  
  132. static const unsigned char perm4[48] =
  133. {
  134.  32, 1, 2, 3, 4, 5,
  135.  4, 5, 6, 7, 8, 9,
  136.  8, 9, 10, 11, 12, 13,
  137.  12, 13, 14, 15, 16, 17,
  138.  16, 17, 18, 19, 20, 21,
  139.  20, 21, 22, 23, 24, 25,
  140.  24, 25, 26, 27, 28, 29,
  141.  28, 29, 30, 31, 32, 1
  142. };
  143.  
  144. static const unsigned char perm5[32] =
  145. {
  146.  16, 7, 20, 21,
  147.  29, 12, 28, 17,
  148.  1, 15, 23, 26,
  149.  5, 18, 31, 10,
  150.  2, 8, 24, 14,
  151.  32, 27, 3, 9,
  152.  19, 13, 30, 6,
  153.  22, 11, 4, 25
  154. };
  155.  
  156. static const unsigned char perm6[64] =
  157. {
  158.  40, 8, 48, 16, 56, 24, 64, 32,
  159.  39, 7, 47, 15, 55, 23, 63, 31,
  160.  38, 6, 46, 14, 54, 22, 62, 30,
  161.  37, 5, 45, 13, 53, 21, 61, 29,
  162.  36, 4, 44, 12, 52, 20, 60, 28,
  163.  35, 3, 43, 11, 51, 19, 59, 27,
  164.  34, 2, 42, 10, 50, 18, 58, 26,
  165.  33, 1, 41, 9, 49, 17, 57, 25
  166. };
  167.  
  168. static const unsigned char sc[16] =
  169. {
  170.  1, 1, 2, 2,
  171.  2, 2, 2, 2,
  172.  1, 2, 2, 2,
  173.  2, 2, 2, 1
  174. };
  175.  
  176. static const unsigned char sbox[8][4][16] =
  177. {
  178.   {
  179.     {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
  180.     {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
  181.     {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
  182.     {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
  183.   },
  184.  
  185.   {
  186.     {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
  187.     {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
  188.     {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
  189.     {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}
  190.   },
  191.  
  192.   {
  193.     {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
  194.     {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
  195.     {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
  196.     {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}
  197.   },
  198.  
  199.   {
  200.     {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
  201.     {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
  202.     {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
  203.     {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}
  204.   },
  205.  
  206.   {
  207.     {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
  208.     {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
  209.     {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
  210.     {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}
  211.   },
  212.  
  213.   {
  214.     {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
  215.     {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
  216.     {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
  217.     {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}
  218.   },
  219.  
  220.   {
  221.     {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
  222.     {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
  223.     {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
  224.     {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}
  225.   },
  226.  
  227.   {
  228.     {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
  229.     {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
  230.     {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
  231.     {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}
  232.   }
  233. };
  234.  
  235. static void
  236. permute (char *out, char *in, const unsigned char * const p, int n)
  237. {
  238.   int i;
  239.  
  240.   for (i = 0; i < n; i++)
  241.     out[i] = in[p[i] - 1];
  242. }
  243.  
  244. static void
  245. left_shift (char *d, int count, int n)
  246. {
  247.   char out[64];
  248.   int i;
  249.  
  250.   for (i = 0; i < n; i++)
  251.     out[i] = d[(i + count) % n];
  252.  
  253.   for (i = 0; i < n; i++)
  254.     d[i] = out[i];
  255. }
  256.  
  257. static void
  258. concat (char *out, char *in1, char *in2, int l1, int l2)
  259. {
  260.   while (l1--)
  261.     (*out++) = (*in1++);
  262.  
  263.   while (l2--)
  264.     (*out++) = (*in2++);
  265. }
  266.  
  267. static void
  268. xor (char *out, char *in1, char *in2, int n)
  269. {
  270.   int i;
  271.  
  272.   for (i = 0; i < n; i++)
  273.     out[i] = in1[i] ^ in2[i];
  274. }
  275.  
  276. static void
  277. dohash (char *out, char *in, char *key, int forw)
  278. {
  279.   int i, j, k;
  280.   char pk1[56];
  281.   char c[28];
  282.   char d[28];
  283.   char cd[56];
  284.   char ki[16][48];
  285.   char pd1[64];
  286.   char l[32], r[32];
  287.   char rl[64];
  288.  
  289.   permute (pk1, key, perm1, 56);
  290.  
  291.   for (i = 0; i < 28; i++)
  292.     c[i] = pk1[i];
  293.  
  294.   for (i = 0; i < 28; i++)
  295.     d[i] = pk1[i + 28];
  296.  
  297.   for (i = 0; i < 16; i++)
  298.   {
  299.     left_shift (c, sc[i], 28);
  300.     left_shift (d, sc[i], 28);
  301.  
  302.     concat (cd, c, d, 28, 28);
  303.     permute (ki[i], cd, perm2, 48);
  304.   }
  305.  
  306.   permute (pd1, in, perm3, 64);
  307.  
  308.   for (j = 0; j < 32; j++)
  309.   {
  310.     l[j] = pd1[j];
  311.     r[j] = pd1[j + 32];
  312.   }
  313.  
  314.   for (i = 0; i < 16; i++)
  315.   {
  316.     char er[48];
  317.     char erk[48];
  318.     char b[8][6];
  319.     char cb[32];
  320.     char pcb[32];
  321.     char r2[32];
  322.  
  323.     permute (er, r, perm4, 48);
  324.  
  325.     xor (erk, er, ki[forw ? i : 15 - i], 48);
  326.  
  327.     for (j = 0; j < 8; j++)
  328.     {
  329.       for (k = 0; k < 6; k++)
  330.         b[j][k] = erk[j * 6 + k];
  331.     }
  332.  
  333.     for (j = 0; j < 8; j++)
  334.     {
  335.       int m, n;
  336.  
  337.       m = (b[j][0] << 1) | b[j][5];
  338.  
  339.       n = (b[j][1] << 3) | (b[j][2] << 2) | (b[j][3] << 1) | b[j][4];
  340.  
  341.       for (k = 0; k < 4; k++)
  342.         b[j][k] = (sbox[j][m][n] & (1 << (3 - k))) ? 1 : 0;
  343.     }
  344.  
  345.     for (j = 0; j < 8; j++)
  346.     {
  347.       for (k = 0; k < 4; k++)
  348.         cb[j * 4 + k] = b[j][k];
  349.     }
  350.  
  351.     permute (pcb, cb, perm5, 32);
  352.  
  353.     xor (r2, l, pcb, 32);
  354.  
  355.     for (j = 0; j < 32; j++)
  356.       l[j] = r[j];
  357.  
  358.     for (j = 0; j < 32; j++)
  359.       r[j] = r2[j];
  360.   }
  361.  
  362.   concat (rl, r, l, 32, 32);
  363.  
  364.   permute (out, rl, perm6, 64);
  365. }
  366.  
  367. static void
  368. str_to_key (unsigned char *str, unsigned char *key)
  369. {
  370.   int i;
  371.  
  372.   key[0] = str[0] >> 1;
  373.   key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2);
  374.   key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3);
  375.   key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4);
  376.   key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5);
  377.   key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6);
  378.   key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7);
  379.   key[7] = str[6] & 0x7F;
  380.  
  381.   for (i = 0; i < 8; i++)
  382.     key[i] = (key[i] << 1);
  383. }
  384.  
  385. static void
  386. smbhash (unsigned char *out, unsigned char *in, unsigned char *key, int forw)
  387. {
  388.   int i;
  389.   char outb[64];
  390.   char inb[64];
  391.   char keyb[64];
  392.   unsigned char key2[8];
  393.  
  394.   str_to_key (key, key2);
  395.  
  396.   for (i = 0; i < 64; i++)
  397.   {
  398.     inb[i] = (in[i / 8] & (1 << (7 - (i % 8)))) ? 1 : 0;
  399.     keyb[i] = (key2[i / 8] & (1 << (7 - (i % 8)))) ? 1 : 0;
  400.     outb[i] = 0;
  401.   }
  402.  
  403.   dohash (outb, inb, keyb, forw);
  404.  
  405.   for (i = 0; i < 8; i++)
  406.     out[i] = 0;
  407.  
  408.   for (i = 0; i < 64; i++)
  409.   {
  410.     if (outb[i])
  411.       out[i / 8] |= (1 << (7 - (i % 8)));
  412.   }
  413. }
  414.  
  415. static void
  416. E_P16 (unsigned char *p14, unsigned char *p16)
  417. {
  418.   unsigned char sp8[8] =
  419.   {
  420.    0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25
  421.   };
  422.  
  423.   smbhash (p16, sp8, p14, 1);
  424.   smbhash (p16 + 8, sp8, p14 + 7, 1);
  425. }
  426.  
  427. static void
  428. E_P24 (unsigned char *p21, unsigned char *c8, unsigned char *p24)
  429. {
  430.   smbhash (p24, c8, p21, 1);
  431.   smbhash (p24 + 8, c8, p21 + 7, 1);
  432.   smbhash (p24 + 16, c8, p21 + 14, 1);
  433. }
  434.  
  435. /****************************************************************************/
  436.  
  437. /*
  438.    This implements the X/Open SMB password encryption
  439.    It takes a password, a 8 byte "crypt key" and puts 24 bytes of
  440.    encrypted password into p24
  441.  */
  442. void
  443. smb_encrypt (unsigned char *passwd, unsigned char *c8, unsigned char *p24)
  444. {
  445.   unsigned char p14[15], p21[21];
  446.   int len;
  447.  
  448.   memset (p21, '\0', sizeof(p21));
  449.   memset (p14, '\0', sizeof(p14));
  450.  
  451.   len = strlen(passwd);
  452.   if(len > sizeof(p14)-1)
  453.     len = sizeof(p14)-1;
  454.  
  455.   memcpy (p14, passwd, len);
  456.  
  457.   StringToUpper ((char *) p14);
  458.   E_P16 (p14, p21);
  459.  
  460.   smb_owf_encrypt (p21, c8, p24);
  461. }
  462.  
  463. /****************************************************************************/
  464.  
  465. /* Routines for Windows NT MD4 Hash functions. */
  466. static int
  467. local_wcslen (short *str)
  468. {
  469.   int len = 0;
  470.  
  471.   while ((*str++) != 0)
  472.     len++;
  473.  
  474.   return len;
  475. }
  476.  
  477. /*
  478.  * Convert a string into an NT UNICODE string.
  479.  * Note that regardless of processor type
  480.  * this must be in intel (little-endian)
  481.  * format.
  482.  */
  483. static int
  484. local_mbstowcs (short *dst, unsigned char *src, int len)
  485. {
  486.   int i;
  487.   short val;
  488.  
  489.   for (i = 0; i < len; i++)
  490.   {
  491.     val = (*src++);
  492.     WSET(dst,0,val);
  493.     dst++;
  494.  
  495.     if (val == 0)
  496.       break;
  497.   }
  498.  
  499.   return i;
  500. }
  501.  
  502. /*
  503.  * Creates the MD4 Hash of the users password in NT UNICODE.
  504.  */
  505. static void
  506. E_md4hash (unsigned char *passwd, unsigned char *p16)
  507. {
  508.   short wpwd[129];
  509.   int len;
  510.  
  511.   /* Password cannot be longer than 128 characters */
  512.   len = strlen ((char *) passwd);
  513.   if (len > 128)
  514.     len = 128;
  515.  
  516.   /* Password must be converted to NT unicode */
  517.   local_mbstowcs (wpwd, passwd, len);
  518.   wpwd[len] = 0; /* Ensure string is null terminated */
  519.  
  520.   /* Calculate length in bytes */
  521.   len = local_wcslen (wpwd) * sizeof (short);
  522.  
  523.   mdfour (p16, (unsigned char *) wpwd, len);
  524. }
  525.  
  526. /* Does the des encryption from the NT or LM MD4 hash. */
  527. static void
  528. smb_owf_encrypt (unsigned char *passwd, unsigned char *c8, unsigned char *p24)
  529. {
  530.   unsigned char p21[21];
  531.  
  532.   memset (p21, '\0', sizeof(p21));
  533.  
  534.   memcpy (p21, passwd, 16);
  535.   E_P24 (p21, c8, p24);
  536. }
  537.  
  538. /****************************************************************************/
  539.  
  540. /* Does the NT MD4 hash then des encryption. */
  541. void
  542. smb_nt_encrypt (unsigned char *passwd, unsigned char *c8, unsigned char *p24)
  543. {
  544.   unsigned char p21[21];
  545.  
  546.   memset (p21, '\0', sizeof(p21));
  547.  
  548.   E_md4hash (passwd, p21);
  549.   smb_owf_encrypt (p21, c8, p24);
  550. }
  551.  
  552. /****************************************************************************/
  553.  
  554. static unsigned long A, B, C, D;
  555.  
  556. static unsigned long
  557. F(unsigned long X, unsigned long Y, unsigned long Z)
  558. {
  559.   return (X & Y) | ((~X) & Z);
  560. }
  561.  
  562. static unsigned long
  563. G(unsigned long X, unsigned long Y, unsigned long Z)
  564. {
  565.   return (X & Y) | (X & Z) | (Y & Z);
  566. }
  567.  
  568. static unsigned long
  569. H(unsigned long X, unsigned long Y, unsigned long Z)
  570. {
  571.   return X ^ Y ^ Z;
  572. }
  573.  
  574. static unsigned long
  575. lshift (unsigned long x, int s)
  576. {
  577.   return (x << s) | (x >> (32 - s));
  578. }
  579.  
  580. #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
  581. #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + 0x5A827999UL,s)
  582. #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1UL,s)
  583.  
  584. /* this applies md4 to 64 byte chunks */
  585. static void
  586. mdfour64 (unsigned long *M)
  587. {
  588.   unsigned long AA, BB, CC, DD;
  589.   unsigned long X[16];
  590.  
  591.   memcpy(X,M,sizeof(X));
  592.  
  593.   AA = A;
  594.   BB = B;
  595.   CC = C;
  596.   DD = D;
  597.  
  598.   ROUND1 (A, B, C, D, 0, 3);
  599.   ROUND1 (D, A, B, C, 1, 7);
  600.   ROUND1 (C, D, A, B, 2, 11);
  601.   ROUND1 (B, C, D, A, 3, 19);
  602.   ROUND1 (A, B, C, D, 4, 3);
  603.   ROUND1 (D, A, B, C, 5, 7);
  604.   ROUND1 (C, D, A, B, 6, 11);
  605.   ROUND1 (B, C, D, A, 7, 19);
  606.   ROUND1 (A, B, C, D, 8, 3);
  607.   ROUND1 (D, A, B, C, 9, 7);
  608.   ROUND1 (C, D, A, B, 10, 11);
  609.   ROUND1 (B, C, D, A, 11, 19);
  610.   ROUND1 (A, B, C, D, 12, 3);
  611.   ROUND1 (D, A, B, C, 13, 7);
  612.   ROUND1 (C, D, A, B, 14, 11);
  613.   ROUND1 (B, C, D, A, 15, 19);
  614.  
  615.   ROUND2 (A, B, C, D, 0, 3);
  616.   ROUND2 (D, A, B, C, 4, 5);
  617.   ROUND2 (C, D, A, B, 8, 9);
  618.   ROUND2 (B, C, D, A, 12, 13);
  619.   ROUND2 (A, B, C, D, 1, 3);
  620.   ROUND2 (D, A, B, C, 5, 5);
  621.   ROUND2 (C, D, A, B, 9, 9);
  622.   ROUND2 (B, C, D, A, 13, 13);
  623.   ROUND2 (A, B, C, D, 2, 3);
  624.   ROUND2 (D, A, B, C, 6, 5);
  625.   ROUND2 (C, D, A, B, 10, 9);
  626.   ROUND2 (B, C, D, A, 14, 13);
  627.   ROUND2 (A, B, C, D, 3, 3);
  628.   ROUND2 (D, A, B, C, 7, 5);
  629.   ROUND2 (C, D, A, B, 11, 9);
  630.   ROUND2 (B, C, D, A, 15, 13);
  631.  
  632.   ROUND3 (A, B, C, D, 0, 3);
  633.   ROUND3 (D, A, B, C, 8, 9);
  634.   ROUND3 (C, D, A, B, 4, 11);
  635.   ROUND3 (B, C, D, A, 12, 15);
  636.   ROUND3 (A, B, C, D, 2, 3);
  637.   ROUND3 (D, A, B, C, 10, 9);
  638.   ROUND3 (C, D, A, B, 6, 11);
  639.   ROUND3 (B, C, D, A, 14, 15);
  640.   ROUND3 (A, B, C, D, 1, 3);
  641.   ROUND3 (D, A, B, C, 9, 9);
  642.   ROUND3 (C, D, A, B, 5, 11);
  643.   ROUND3 (B, C, D, A, 13, 15);
  644.   ROUND3 (A, B, C, D, 3, 3);
  645.   ROUND3 (D, A, B, C, 11, 9);
  646.   ROUND3 (C, D, A, B, 7, 11);
  647.   ROUND3 (B, C, D, A, 15, 15);
  648.  
  649.   A += AA;
  650.   B += BB;
  651.   C += CC;
  652.   D += DD;
  653. }
  654.  
  655. static void
  656. copy64 (unsigned long *M, unsigned char *in)
  657. {
  658.   int i;
  659.  
  660.   for (i = 0; i < 16; i++)
  661.   {
  662.     M[i] = (in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) |
  663.       (in[i * 4 + 1] << 8) | (in[i * 4 + 0] << 0);
  664.   }
  665. }
  666.  
  667. static void
  668. copy4 (unsigned char *out, unsigned long x)
  669. {
  670.   out[0] = x & 0xFF;
  671.   out[1] = (x >> 8) & 0xFF;
  672.   out[2] = (x >> 16) & 0xFF;
  673.   out[3] = (x >> 24) & 0xFF;
  674. }
  675.  
  676. /* produce a md4 message digest from data of length n bytes */
  677. static void
  678. mdfour (unsigned char *out, unsigned char *in, int n)
  679. {
  680.   unsigned char buf[128];
  681.   unsigned long M[16];
  682.   unsigned long b = n * 8;
  683.  
  684.   A = 0x67452301;
  685.   B = 0xefcdab89;
  686.   C = 0x98badcfe;
  687.   D = 0x10325476;
  688.  
  689.   while (n > 64)
  690.   {
  691.     copy64 (M, in);
  692.     mdfour64 (M);
  693.  
  694.     in += 64;
  695.     n -= 64;
  696.   }
  697.  
  698.   memset (buf, 0, sizeof(buf));
  699.   memcpy (buf, in, n);
  700.   buf[n] = 0x80;
  701.  
  702.   if (n <= 55)
  703.   {
  704.     copy4 (buf + 56, b);
  705.     copy64 (M, buf);
  706.     mdfour64 (M);
  707.   }
  708.   else
  709.   {
  710.     copy4 (buf + 120, b);
  711.     copy64 (M, buf);
  712.     mdfour64 (M);
  713.     copy64 (M, buf + 64);
  714.     mdfour64 (M);
  715.   }
  716.  
  717.   memset (buf, 0, sizeof(buf));
  718.   copy64 (M, buf);
  719.  
  720.   copy4 (out, A);
  721.   copy4 (out + 4, B);
  722.   copy4 (out + 8, C);
  723.   copy4 (out + 12, D);
  724. }
  725.